/* UVa 10183 - Traditional BINGO */
#include<iostream>
#include<cstring>
using namespace std;

int card[6][6];
bool bingoed = false;

bool bingo() {
	int row[6] = { 0 }, col[6] = { 0 }, diag = 0, backdiag = 0;
	for (int i = 0; i < 5; ++i) {
		for (int j = 0; j < 5; ++j) {
			row[i] |= card[i][j];
			col[j] |= card[i][j];
		}
		diag |= card[i][i];
		backdiag |= card[4 - i][i];
	}
	if (diag == 0 || backdiag == 0) {
		bingoed = true;
		return bingoed;
	}
	for (int i = 0; i < 5; ++i) {
		if (row[i] == 0 || col[i] == 0) {
			bingoed = true;
			return bingoed;
		}
	}
	return false;
}

void solve() {
	// card[3][3] = 0;
	for (int i = 0; i < 5; ++i) {
		for (int j = 0; j < 5; ++j) {
			if (i != 2 || j != 2) cin >> card[j][i];
		}
	}
  	for (int n = 1, i = 0; n <= 75; ++n) {
		int num; cin >> num;
		if (bingoed) continue;
		if (1 <= num && num <= 15) i = 0;
		else if (16 <= num && num <= 30) i = 1;
		else if (31 <= num && num <= 45) i = 2;
		else if (46 <= num && num <= 60) i = 3;
		else if (61 <= num && num <= 75) i = 4;
		for (int j = 0; j < 5; ++j) {
			if (card[i][j] == num) {
				card[i][j] = 0;
				break;
			}
		}
		if (not bingoed and bingo()) {
			cout << "BINGO after " << n << " numbers announced\n";
		}
	}
	return;
}

int main() {
	int tc; cin >> tc;
	for (int t = 1; t <= tc; ++t) {
		memset(card, 0, sizeof(card));
		bingoed = false;
		solve();
	}
}
